package com.hui.controller;


import com.alibaba.fastjson.JSONObject;
import com.hui.constant.IndexConstants;
import com.hui.entity.enums.ProductEnum;
import com.hui.service.StoreProductService;
import com.hui.service.SystemGroupDataService;
import com.hui.util.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页的接口控制层
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/api")
public class IndexController {
    //组合数据详情表(SystemGroupData)
    private final SystemGroupDataService systemGroupDataService;

    //商品表(StoreProduct)
    private final StoreProductService storeProductService;

    /**
     * 获取首页轮播图
     *
     * @return
     */
    @GetMapping("/index/banner")
    public ResponseResult getBanner() {

        return systemGroupDataService.getDatas(IndexConstants.YSHOP_HOME_BANNER);
    }

    /**
     * 获取四个小菜单
     *
     * @return
     */
    @GetMapping("/index/menus")
    public ResponseResult getMenus() {
        return systemGroupDataService.getDatas(IndexConstants.YSHOP_HOME_MENUS);
    }

    /**
     * 获取首页搜索热门搜索关键字
     *
     * @return
     */
    @GetMapping("/search/keyword")
    public ResponseResult getKeyword() {
        ResponseResult datas = systemGroupDataService.getDatas(IndexConstants.YSHOP_HOT_SEARCH);
        ArrayList<JSONObject> list = (ArrayList) datas.getData();
        List<String> stringList = new ArrayList<>();
        for (JSONObject object : list) {
            stringList.add(object.getString("title"));
        }
        return ResponseResult.okResult(stringList);
    }


    /**
     * 获取首页精品推荐
     *
     * @return
     */
    @GetMapping("/index/boutiqueList")
    public ResponseResult getboutiqueList() {
        return storeProductService.getList(IndexConstants.BOUTIQUELIST_PAGE, IndexConstants.BOUTIQUELIST_LIMIT, ProductEnum.TYPE_1.getValue(), 0);
    }

    /**
     * 获取首页猜你喜欢
     *
     * @return
     */
    @GetMapping("/index/like")
    public ResponseResult getLike() {
        return storeProductService.getList(IndexConstants.LIKE_PAGE, IndexConstants.LIKE_LIMIT, ProductEnum.TYPE_4.getValue(), 0);
    }
}
